<template>
  <div class="add">
    <el-dialog
      :title="{ add: '新增学科', edit: '编辑学科' }[type]"
      :visible="value"
      @close="closeEvent"
    >
      <el-form ref="form" label-width="80px" :model="form" :rules="rules">
        <el-form-item label="学科编号" prop="rid">
          <el-input v-model="form.rid" />
        </el-form-item>
        <el-form-item label="学科名称" prop="name">
          <el-input v-model.trim="form.name" />
        </el-form-item>
        <el-form-item label="学科简称">
          <el-input v-model="form.short_name" />
        </el-form-item>
        <el-form-item label="学科简介">
          <el-input v-model="form.intro" />
        </el-form-item>
        <el-form-item label="学科备注">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { subjectAdd, subjectEdit } from '@/api/subject'
export default {
  props: {
    value: {
      type: Boolean,
      required: true
    },
    initList: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      type: 'add',
      show: false,
      form: {
        rid: '', //	是	string	学科编号
        name: '', //	是	string	学科名称
        short_name: '', //	否	string	学科简称
        intro: '', //	否	string	学科简介
        remark: '' //
      },
      rules: {
        rid: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (this.type === 'add') {
                const newArr = this.initList.filter((item) => {
                  return item.rid === this.form.rid
                })
                console.log('new', newArr)
                // console.log('form', this.form)
                // console.log('initList', this.initList)
                const bol = newArr.every((item) => item.rid !== value)
                bol ? callback() : callback(new Error('学科编码已存在' + value))
              } else if (this.type === 'edit') {
                const newRid = this.initList.filter(
                  (item) => item.rid !== this.form.rid
                )
                const bol = newRid.every((item) => item.rid !== value)
                bol ? callback() : callback(new Error('学科编码已存在' + value))
              }
            }
          }
        ],
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (this.type === 'add') {
                const bol = this.initList.some((item) => item.name === value)
                bol ? callback(new Error('学科已存在' + value)) : callback()
              } else if (this.type === 'edit') {
                const newName = this.initList.filter(
                  (item) => item.name !== this.form.name
                )
                const bol = newName.every((item) => item.name !== value)
                bol ? callback() : callback(new Error('学科已存在' + value))
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    // 弹框关闭事件
    closeEvent() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        rid: '', //	是	string	学科编号
        name: '', //	是	string	学科名称
        short_name: '', //	否	string	学科简称
        intro: '', //	否	string	学科简介
        remark: ''
      }
    },
    // 新增、编辑事件
    async submit() {
      await this.$refs.form.validate()
      if (this.type === 'add') {
        const res = await subjectAdd(this.form)
        // if (res.code === 201) {
        //   this.$message.error(res.message)
        // }
        //  else {
        this.$emit('input', false)
        this.$message.success('新增学科成功')
        console.log('新增学科', res)
        // }
      } else if (this.type === 'edit') {
        const res = await subjectEdit(this.form)
        console.log('编辑', res)
        this.$message.success('编辑学科成功')
      }
      this.$emit('input', false)
      this.$emit('getData')
    }
  }
}
</script>

<style></style>
